package com.jxzyk.model.center;

import com.core.ui.fragment.BaseFragment;
import com.jxzyk.R;
import com.jxzyk.model.users.LoginActivity;
import com.jxzyk.util.BitmapHelp;
import com.jxzyk.util.UserManger;
import com.jxzyk.view.TitleLayout;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class CenterFragment extends BaseFragment implements OnClickListener {
	private UserManger manger;
	private Class<?>[] classes = new Class<?>[] { PersonDataActivity.class, LeaningRecordActivity.class,
			MyCoursesActivity.class, MyNoteActivity.class };
	private String[] titles = new String[] { "个人资料", "学习记录", "我的课程", "我的笔记" };
	private String[] surl = new String[] { "http://210.28.101.76:8080/doc/image/learn/3.jpg",
			"http://210.28.101.76:8080/doc/image/learn/4.jpg", "http://210.28.101.76:8080/doc/image/learn/1.jpg",
			"http://210.28.101.76:8080/doc/image/learn/2.jpg" };

	@Override
	public void initData(Bundle bundle) {

	}

	@Override
	public int initUiRes() {
		return R.layout.fra_center;
	}

	@ViewInject(R.id.title)
	TitleLayout titlelayout;
	@ViewInject(R.id.tv_center_exitlogin)
	Button exit;
	@ViewInject(R.id.gv_center)
	GridView gv;

	@Override
	public void initUi(View view) {
		gv.setAdapter(new gvAdapter());
		titlelayout.setTtile("学习中心");
		exit.setOnClickListener(this);
		gv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				showActivity(classes[position]);
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_center_exitlogin:
			new UserManger(getActivity()).setAuto(false);
			showActivity(LoginActivity.class);
			getActivity().finish();
			break;
		}
	}

	public class gvAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 4;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				convertView = View.inflate(getActivity(), R.layout.item_grid, null);
			}
			ImageView iv = (ImageView) convertView.findViewById(R.id.img_item_grid);
			WindowManager manager = getActivity().getWindowManager();
			LayoutParams params = iv.getLayoutParams();
			params.width = (int) (manager.getDefaultDisplay().getWidth() * 0.25);
			params.height=params.width;
			iv.setLayoutParams(params);
			BitmapUtils bitmapUtils = BitmapHelp.getBitmapUtils(getActivity());
			bitmapUtils.display(iv, surl[position]);
			TextView tv = (TextView) convertView.findViewById(R.id.tv_item_grid);
			tv.setText(titles[position]);
			return convertView;
		}

	}
}
